package pl.edu.agh.iosr.gamblingzone.action;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

import pl.edu.agh.iosr.gamblingzone.dao.UserDAO;
import pl.edu.agh.iosr.gamblingzone.dao.ejb.UserDAOBean;
import pl.edu.agh.iosr.gamblingzone.model.User;

@Stateless
@Scope(ScopeType.PAGE)
@Name("userManage")
public class UserManageAction implements UsersManage {

	@PersistenceContext
	EntityManager entityManager;
	
	private List<User> allUsers;
	
	private String test;
	
	private UserDAO userDAO;
	
	private String currentAction;
	
	private User details;
	
	@Logger
	Log log;

	@Out
	public List<User> getAllUsers() {
		log.info("getAllUsers");
		setupDAO();
		
		allUsers = userDAO.findAll();
		log.info("# of users: " + allUsers.size());
		return allUsers;
	}

	@Out
	public String getTest() {
		log.info("getTest");
		if (test == null) {
			test = "aaaaa";
		}
		return test;
	}

	public void setCurrentAction(String currentAction) {
		log.info("Action");
		this.currentAction = currentAction;
	}

	public void removeUser(User u) {
		log.info("Removing object");
		setupDAO();
		userDAO.makeTransient(u);
	}
	
	private void setupDAO() {
		if (userDAO == null) {
			userDAO = new UserDAOBean();
			((UserDAOBean) userDAO).setEntityManager(entityManager);
		}
	}

	public void blockUser(User u) {
		setupDAO();
		u.setBlocked(true);
	}

	public void unblockUser(User u) {
		setupDAO();
		u.setBlocked(false);
	}

	public User getDetails() {
		return details;
	}

	public void setDetails(User details) {
		this.details = details;
	}
}
